package com.ljxy.score.util;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.extension.service.IService;
import com.ljxy.score.entity.ExcelData;
import com.ljxy.score.handler.ExcelListener;
import com.ljxy.score.service.myscoreService;
import com.ljxy.score.service.mystudentService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//在这里其实进行了一步泛型的处理 我们在上传的时候其实就是这样的
//把你想简化的东西就可以封装成一个工具类 到时候我们就可以进行直接使用就行
public class ExcelUtil {
    public static void download(HttpServletResponse response, Class t, List list, String filename) throws IOException {
        response.setContentType("application/vnd.ms-excel");// 设置文本内省
        response.setCharacterEncoding("utf-8");// 设置字符编码
        response.setHeader("Content-disposition", "attachment;filename=" + filename + ".xlsx"); // 设置响应头
        EasyExcel.write(response.getOutputStream(), t).sheet("模板").doWrite(list); //用io流来写入数据
    }

    public static void upload(myscoreService myscoreService, mystudentService mystudentService, MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), ExcelData.class, new ExcelListener(mystudentService, myscoreService)).sheet().doRead();
    }
}